package com.fancy.projecty.A_流程相关;

import com.fancy.projecty.util.UuidUtil;
import org.activiti.engine.RepositoryService;
import org.activiti.engine.RuntimeService;
import org.activiti.engine.TaskService;
import org.activiti.engine.repository.Deployment;
import org.junit.Test;
import org.junit.runner.RunWith;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.boot.test.context.SpringBootTest;
import org.springframework.test.context.junit4.SpringRunner;

import java.io.InputStream;
import java.util.List;
import java.util.zip.ZipInputStream;


/**
 * 部署流程
 * <p>
 * 部署成功之后会生成25张表 ,
 * 然后 ACT_RE_DEPLOYMENT 这个表就会增加一条当前部署的记录信息
 */
@RunWith(SpringRunner.class)
@SpringBootTest
public class A_部署流程图 {
	@Autowired
	/*管理流程定义,部署相关的Service*/
	private RepositoryService repositoryService;
	@Autowired
	/*执行管理、包括启动、推进、删除流程实例等操作*/
	private RuntimeService runtimeService;
	@Autowired
	/*任务管理,可以做任务的查询的*/
	private TaskService taskService;





	/**
	 * zip文件方式部署的
	 * 部署的信息主要添加到act_re_deployment 表里面
	 */
	@Test
	public void c_zip方式部署() {
		InputStream resourceAsStream =
				this.getClass().getClassLoader().getResourceAsStream("diagram/holiday.zip");
		ZipInputStream zipInputStream = new ZipInputStream(resourceAsStream);
		Deployment deployment = this.repositoryService.createDeployment()
				.addZipInputStream(zipInputStream)
				.name("请假方案1")
				.tenantId(UuidUtil.getUUID32()) // 设置 tenantId  可以重复  .
				.category("请假流程")  //设置流程定义的类别, 可以不设置
//				.tenantId("1") // 设置
				.deploy();

		System.out.println(deployment.getId());  //1
		System.out.println(deployment.getName());  //请假单申请流程
		System.out.println(deployment.getCategory());  //null
		System.out.println(deployment.getDeploymentTime()); // Tue Dec 31 16:45:13 CST 2019
		System.out.println(deployment.getTenantId());


	}

	/**
	 * 查询的是这个表的数据act_re_deployment
	 */
	@Test
	public void c_查询部署的流程() {
		List<Deployment> list = this.repositoryService.createDeploymentQuery().deploymentCategory("请假类别").list();
		System.out.println("list = " + list);
	}


	/**
	 * 部署成功之后会生成25张表
	 */
	@Test
	public void c_1单个文件部署流程() {
		//得到repositoryService 就可以操作部署相关的操作了.
		//1. 添加资源, png资源可以不添加,但是bpmn资源是必须要添加的
		Deployment deployment = this.repositoryService.createDeployment()
				.addClasspathResource("a项目实战/请假流程1.bpmn")
//				.addClasspathResource("diagram/holiday.png")
				//给流程添加名字
				.name("请假方案2")
				.category("请假类别")
				//开始部署
				.deploy();
		System.out.println(deployment.getName());
		System.out.println(deployment.getId());
	}

}
